package com.example.educationplatformbackend.controller;


import com.example.educationplatformbackend.domian.UserBean.CompleteUser;
import com.example.educationplatformbackend.domian.UserBean.LoginUser;
import com.example.educationplatformbackend.domian.UserBean.RegisterUser;
import com.example.educationplatformbackend.domian.UserBean.User;
import com.example.educationplatformbackend.service.Impl.UserService;
import com.example.educationplatformbackend.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userServicel;


    @PostMapping("login")
    public ResponseResult<User> login(@RequestBody LoginUser loginUser) {
        User user = userServicel.login(loginUser); //查询用户信息

        if (user != null) {
            return ResponseResult.success(user);
        } else {
            return ResponseResult.failure(HttpStatus.BAD_REQUEST,"账号或密码错误！");
        }
    }

    @PostMapping("register")
    public ResponseResult<Object> register(@RequestBody RegisterUser registerUser){
         userServicel.register(registerUser);

         return ResponseResult.success("注册成功");
    }



    @GetMapping("all")
    public ResponseResult<List<CompleteUser>> getuser(){
        return ResponseResult.success(userServicel.getAlluser());
    }


    @PostMapping("update")
    public ResponseResult<Object> update(@RequestBody CompleteUser completeUser){
        userServicel.update(completeUser);
        return ResponseResult.success("更新成功");
    }


    @GetMapping("delete")
    public ResponseResult<Object> deleteById(int id){
        userServicel.deleteById(id);
        return ResponseResult.success("删除成功");
    }
}
